package com.hemh.service;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;
import com.hemh.dao.OmsUserMapper;
import com.hemh.entity.OmsUser;
import com.hemh.entity.OmsUserExample;
import com.hemh.utils.AESUtil;

@Service
public class UserService {
	
	private final Log logger = LogFactory.getLog(UserService.class);

	@Resource
	private OmsUserMapper omsUserMapper;
	
	/**
	 * @author HeMingHui
	 * @since 2020-10-21
	 * @param usernumber
	 * @param password
	 * @return Map<String, String>
	 * @throws Exception 
	 * @description 用户登录
	 */
	public Map<String, String> userLogin(String usernumber, String password) throws Exception {
		OmsUserExample example = new OmsUserExample();
		example.createCriteria()
			.andUserNumberEqualTo(usernumber)
			.andPasswordEqualTo(AESUtil.encrypt(password));
		OmsUser user = omsUserMapper.selectOneByExample(example);
		Map<String, String> loginResult = new HashMap<String, String>();
		if(null != user) {
			loginResult.put("retcode", "200");
			loginResult.put("retmsg", "login success");
		}else {
			loginResult.put("retcode", "404");
			loginResult.put("retmsg", "login failed");
		}			
		return loginResult;
	}
	
	/**
	 * @author HeMingHui
	 * @since 2020-10-24
	 * @param user
	 * @return int
	 * @description 用户注册
	 */
	public int userRegister(OmsUser user) {
		int registerCode = 0;/**默认注册不成功*/
		boolean isExist = false; /** 默认注册的用户不存在 */
		isExist = this.userIsExist(user.getUserNumber());
		if(isExist) {/** 如果存在, 不允许插入用户 */
			logger.info("--->>>user_number = "+user.getUserNumber()+", 用户已经存在!");
			return registerCode;
		}
		registerCode = this.insertUser(user);
		logger.info("--->>>用户插入结果, registerCode = " + registerCode);
		return registerCode;
	}
	
	/**
	 * @author HeMingHui
	 * @since 2020-10-24
	 * @param userNumber
	 * @return boolean
	 * @description 查询用户是否存在
	 */
	public boolean userIsExist(String userNumber) {
		boolean flag = false;/** 默认不存在 */
		OmsUserExample example = new OmsUserExample();
		example.createCriteria().andUserNumberEqualTo(userNumber);
		int result = (int) omsUserMapper.countByExample(example);
		flag = result>=1?true:false;
		return flag;		
	}
	
	/**
	 * @author HeMingHui
	 * @since 2020-10-24
	 * @param user
	 * @return int: 0-失败, 1-成功
	 * @description 用户插入
	 */
	public int insertUser(OmsUser user) {
		int insertCode = 0;
		user.setDeleted(false);/** 默认不删除 */
		insertCode = omsUserMapper.insert(user);
		return insertCode;
	}
		
	/**
	 * @author HeMingHui
	 * @since 2020-10-24
	 * @param usernumber
	 * @param oldPassword
	 * @return int
	 * @throws Exception 
	 * @description 用户密码是否匹配
	 */
	public boolean isMatchPassword(String usernumber, String oldPassword) throws Exception {
		boolean flag = true; /** 默认匹配 */
		OmsUserExample example = new OmsUserExample();
		example.createCriteria()
			.andUserNumberEqualTo(usernumber)
			.andPasswordEqualTo(AESUtil.encrypt(oldPassword));
		int matchResult = (int) omsUserMapper.countByExample(example);
		flag = matchResult>=1?true:false;
		return flag;		
	}	
	
	/**
	 * @author HeMingHui
	 * @since 2020-10-24
	 * @return String
	 * @throws Exception 
	 * @description 用户更新密码
	 */
	public String updateUserPassword(String usernumber, 
			String oldPassword, 
			String newPassword) throws Exception {	
		OmsUserExample example = new OmsUserExample();
		example.createCriteria().andUserNumberEqualTo(usernumber);
		OmsUser user = omsUserMapper.selectOneByExample(example);		
		if(user == null) {
			return "用户不存在!";
		}
		boolean flag = this.isMatchPassword(usernumber, oldPassword);		
		if(flag) {/**原密码正确*/
			user.setPassword(AESUtil.encrypt(newPassword));
			int result = omsUserMapper.updateByPrimaryKey(user);
			return result >=1?"更新成功":"更新失败";
		}else {
			return "原密码不正确!";
		}
	}
	
	/**
	 * @author HeMingHui
	 * @since 2020-11-03
	 * @param usernumber 
	 * @return OmsUser
	 * @description 根据用户编号查询用户信息
	 */
	public OmsUser getUserInfoByUserNumber(String usernumber) {
		OmsUser userInfo = new OmsUser();
		OmsUserExample example = new OmsUserExample();
		example.createCriteria().andUserNumberEqualTo(usernumber);
		userInfo = omsUserMapper.selectOneByExample(example);
		return userInfo;
	}
}
